<?php
namespace admin\models;

use common\models\RecommendBook;
use common\models\SearchInterface;
use common\models\SearchTrait;

class RecommendBookSearch extends RecommendBook implements SearchInterface
{
    use SearchTrait;

    public $keyword;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['keyword'], 'trim'],
            ['recommend_id', 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function prepareQuery($query)
    {
        $query->andFilterWhere(['recommend_id' => $this->recommend_id]);

        // 关键词
        if ($this->keyword) {
            $query->joinWith('book')
                ->andFilterWhere([
                'OR',
                ['like', 'name', $this->keyword],
                ['like', 'author', $this->keyword],
            ]);
        }

        return $query->orderBy('display_order desc');
    }
}
